from django.conf.urls.defaults import *
from content.views import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()


urlpatterns = patterns('', 
    ('^$', index),
    (r'^about/$', about),
    (r'^usage/$', usage),
)


#rest part

from django_restapi.resource import Resource
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse
from django_restapi.authentication import HttpBasicAuthentication
from content.marvin import create_example_links, create_example_keywords, get_keywords_by_id, get_links_by_id, update_content, refresh_links
from content.models import delete_keyword, create_keyword, update_keyword
import simplejson as json

from content.marvin import get_group_links
from django.template import RequestContext

class GroupLinks(Resource):
	def read(self,request, id, group):
		list = get_group_links(int(id.encode('ascii')),group)
		return render_to_response('advanced/grouplinks.html', {'list':list, 'group':group}, context_instance=RequestContext(request))

urlpatterns += patterns( '',  
	url(r'^grouplinks/(?P<id>[a-zA-Z0-9]+)/(?P<group>[a-zA-Z0-9]+)$', GroupLinks(permitted_methods=('GET',))),
)

class Contents(Resource):
	"""
	Resource for accepting content and context from client
	"""	
	def create(self, request):
		try:
			requestJson = json.loads(request.raw_post_data)
		except ValueError:
			raise Http404
		id = requestJson['id']
		content = requestJson['content']
		id = update_content(int(id), content)
		return render_to_response('json/id.json', {'id':id,}) #,  mimetype="application/json")

class Keywords(Resource):
	"""
	Resource for getting keywords
	"""	
	def read(self, request, id):
		list = get_keywords_by_id(int(id.encode('ascii'))) #create_example_keywords()
		return render_to_response('json/keywords.json', {'list':list,})#,  mimetype="application/json")
	"""
	Resource for handling keywords
	"""	
	def create(self, request):
		try:
			requestJson = json.loads(request.raw_post_data)
		except ValueError:
			raise Http404
		id = requestJson['id']
		key = requestJson['keyword']
		prior = int(requestJson['priority'])
		create_keyword(id=int(id), keyword=key, priority=prior)
		return HttpResponse(status=200)
	
	def update(self, request):
		try:
			requestJson = json.loads(request.raw_post_data)
		except ValueError:
			raise Http404
		id = requestJson['id']
		oldK = requestJson['oldKeyword']
		prior = int(requestJson['priority'])
		key = requestJson['keyword']
		update_keyword(id=int(id), oldKey=oldK, newKey=key, priority=prior)
		return HttpResponse(status=200)
	
	def delete(self, request):
		try:
			requestJson = json.loads(request.raw_post_data)
		except ValueError:
			raise Http404
		id = requestJson['id']
		delK = requestJson['keyword']
		delete_keyword(id=int(id), keyword=delK)
		return HttpResponse(status=200)
		
class Searches(Resource):
	"""
	Resource for returning links
	"""	
	def read(self, request, id):
		list = get_links_by_id(int(id.encode('ascii'))) #create_example_links()
		return render_to_response('json/links.json', {'list':list,},  mimetype="application/json")

urlpatterns += patterns( '',  
	url(r'^json/content$', Contents(permitted_methods=('POST',))),
	url(r'^json/keyword/(?P<id>[a-zA-Z0-9]+)$', Keywords(permitted_methods=('GET',))),
	url(r'^json/keyword$', Keywords(permitted_methods=('GET', 'POST','PUT','DELETE'))),
	url(r'^json/search/(?P<id>[a-zA-Z0-9]+)$', Searches(permitted_methods=('GET',))),
)
